package com.yu.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yu.mall.dto.BaseDTO;
import com.yu.mall.entity.FootprintCollection;
import com.yu.mall.entity.ProductInfo;
import com.yu.mall.mapper.FootprintCollectionMapper;
import com.yu.mall.mapper.ProductInfoMapper;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.ResultVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("CardVolume")
@Api(tags = "收藏与足迹管理", value = "卡收藏与足迹管理")
public class FootprintCollectionController {

	@Autowired
	private FootprintCollectionMapper footprintCollectionMapper;

	@Autowired
	private ProductInfoMapper productInfoMapper;
	
	
    @PostMapping("addFootprintCollection")
    @ApiOperation(value = "添加足迹或收藏")
    public ResultVO addFootprintCollection(FootprintCollection footprintCollection,BaseDTO baseDTO) {
	   footprintCollection.setOpenid(baseDTO.getOpenid());
//    	   footprintCollection.setOpenid("11");
		List<FootprintCollection> select = footprintCollectionMapper.select(footprintCollection);
		if(select.size()>0){
			footprintCollectionMapper.delete(select.get(0));
			ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(footprintCollection.getSkuId());
			if(productInfo !=null) {
				footprintCollection.setImagePath(productInfo.getImagePath());
				footprintCollection.setJdPrice(productInfo.getJdPrice());
				footprintCollection.setName(productInfo.getName());
				footprintCollectionMapper.insert(footprintCollection);
			}
		}else{
			ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(footprintCollection.getSkuId());
			if(productInfo !=null) {
			footprintCollection.setImagePath(productInfo.getImagePath());
			footprintCollection.setJdPrice(productInfo.getJdPrice());
			footprintCollection.setName(productInfo.getName());
			footprintCollectionMapper.insert(footprintCollection);
			}
		}
	   return ResultUtils.success("添加成功");
    }
    
    @PostMapping("queryFootprintCollectionList")
    @ApiOperation(value = "查询足迹或收藏列表")
    @ApiImplicitParam(name = "status",value="1:收藏 2:足迹",required=true)
    public ResultVO queryFootprintCollectionList(String status,BaseDTO baseDTO) {
	   FootprintCollection footprintCollection = new FootprintCollection();
	   footprintCollection.setOpenid(baseDTO.getOpenid());
//    	   footprintCollection.setOpenid("11");
	   footprintCollection.setStatus(status);
	   Example example = new Example(FootprintCollection.class);
	   Example.Criteria createCriteria = example.createCriteria();
	   createCriteria.andEqualTo("openid", baseDTO.getOpenid());
	   createCriteria.andEqualTo("status", status);
	   example.setOrderByClause("id desc");
	   List<FootprintCollection> select = footprintCollectionMapper.selectByExample(example);
	   return ResultUtils.success(select);
    }
    
    @PostMapping("delFootprintCollection")
    @ApiOperation(value = "取消收藏")
    @ApiImplicitParam(name = "skuId",value="商品id",required=true)
    public ResultVO delFootprintCollection(String skuId,BaseDTO baseDTO) {
    	   FootprintCollection footprintCollection = new FootprintCollection();
    	   footprintCollection.setOpenid(baseDTO.getOpenid());
//    	   footprintCollection.setOpenid("11");
    	   footprintCollection.setSkuId(skuId);
    	   footprintCollection.setStatus("1");
    	   footprintCollectionMapper.delete(footprintCollection);
           return ResultUtils.success("取消成功");
    }
    
    @PostMapping("queryCollection")
    @ApiOperation(value = "查询是否收藏")
    @ApiImplicitParam(name = "skuId",value="商品id",required=true)
    public ResultVO queryCollection(String skuId,BaseDTO baseDTO) {
	   FootprintCollection footprintCollection = new FootprintCollection();
	   footprintCollection.setOpenid(baseDTO.getOpenid());
//    	   footprintCollection.setOpenid("11");
	   footprintCollection.setSkuId(skuId);
	   footprintCollection.setStatus("1");
	   List<FootprintCollection> select = footprintCollectionMapper.select(footprintCollection);
	   if(select.size()>0) {
		   return ResultUtils.success("已收藏");
	   }else {
		   return ResultUtils.success("未收藏");
	   }
    }
    
}
